package com.leo.foodmaster.admin.purchase.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.foodMaterial.entity.FoodMaterial;
import com.leo.foodmaster.admin.purchase.entity.Supplier;
import com.leo.foodmaster.admin.purchase.service.ISupplierService;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.util.ExcelUtil;
import com.leo.foodmaster.commons.util.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 供应商前端控制器
 * </p>
 *
 * @author Leo
 * @since 2021-07-31
 */
@RestController
@AllArgsConstructor
@Api(tags = "供应商管理")
@RequestMapping("/purchase/supplier")
public class SupplierController {
    private final ISupplierService supplierService;

    @ApiOperation("条件分页查询供应商列表")
    @GetMapping("/list")
    public Result<IPage<Supplier>> getSupplierPageList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "供应商名称") @RequestParam(required = false) String name,
            @ApiParam(value = "供应商地址") @RequestParam(required = false) String address,
            @ApiParam(value = "供应商状态") @RequestParam(required = false) String status) {
        IPage<Supplier> pageList  = supplierService.getSupplierPageList(new Page<>(pageNum, pageSize), name,
                address, status);
        return Result.success(pageList);
    }

    @PostMapping
    @ApiOperation("新增供应商")
    public Result<Object> addSupplier(@RequestBody Supplier supplier) {
        return supplierService.addSupplier(supplier);
    }

    @PutMapping
    @ApiOperation("修改供应商")
    public Result<Object> updateSupplier(@RequestBody Supplier supplier) {
        return supplierService.updateSupplier(supplier);
    }

    @GetMapping("/downloadTemplate")
    @ApiOperation("下载供应商模板")
    public Result<Object> downloadTemplate() {
        List<Supplier> list = new ArrayList<>();
        ExcelUtil<Supplier> util = new ExcelUtil<>(Supplier.class);
        return util.exportExcel(list, "供应商模板");
    }

    @PostMapping("/import")
    @ApiOperation("导入供应商数据")
    public Result<Object> importSupplier(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Supplier> util = new ExcelUtil<>(Supplier.class);
        List<Supplier> supplierList = util.importExcel(file.getInputStream());
        String message = supplierService.importSupplier(supplierList, updateSupport);
        return Result.success(message);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除供应商")
    public Result<Object> deleteSupplier(@ApiParam("食材id") @PathVariable Long id) {
        return supplierService.deleteSupplier(id);
    }

}

